package com.wex.posture;

import android.app.*;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.*;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 
{
    private SocketClient socketGyr = null;
    private SocketClient socketAcc = null;
    private SocketClient socketPry = null;
    private SocketServer server = null;
    private boolean socketRun = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
/*
        server = new SocketServer(1201, 1024, new SocketServer.Event() {
                int count = 0, total = 0;
                @Override
                public void clientAccept(int id)
                {
                    Toast.makeText(MainActivity.this, "server accept " + id, Toast.LENGTH_SHORT).show();
                }
                @Override
                public void clientRemove(int id)
                {
                    count = total = 0;
                    Toast.makeText(MainActivity.this, "server remove " + id, Toast.LENGTH_SHORT).show();
                }
                @Override
                public void clientRecv(int id, byte[] b)
                {
                    count += b.length;
                    total += b.length;
                    if (count > 12 * 600)
                    {
                        count = 0;
                        Toast.makeText(MainActivity.this, String.format("server %d recv %.2fK", id, (float)total / 1024), Toast.LENGTH_SHORT).show();
                    }
                }
            });*/

        SensorManager sm = (SensorManager)this.getSystemService(Context.SENSOR_SERVICE);

        //加速度计
        Sensor accel = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(new SensorEventListener() {
                TextView tv1 = findViewById(R.id.tv1);
                TextView tv2 = findViewById(R.id.tv2);
                TextView tv3 = findViewById(R.id.tv3);
                byte[] stream = new byte[12];
                byte[] order = new byte[]{0, 1, 2};//轴顺序纠正
                int[] dir = new int[]{1, 1, 1};//轴数值方向纠正
                @Override
                public void onSensorChanged(SensorEvent ev)
                {
                    tv1.setText(String.format("accX: %12.6f", ev.values[0]));
                    tv2.setText(String.format("accY: %12.6f", ev.values[1]));
                    tv3.setText(String.format("accZ: %12.6f", ev.values[2]));
                    //转换float数组为byte数组
                    int count = 0;
                    for (int i = 0; i < order.length; i++)
                    {
                        int value = Float.floatToIntBits(ev.values[order[i]] / 9.8f * dir[i]);
                        stream[count++] = (byte)(value & 0xFF);
                        stream[count++] = (byte)((value >> 8) & 0xFF);
                        stream[count++] = (byte)((value >> 16) & 0xFF);
                        stream[count++] = (byte)((value >> 24) & 0xFF);
                    }
                    if (socketAcc != null)
                        socketAcc.send(stream);
                }
                @Override
                public void onAccuracyChanged(Sensor s, int accuracy)
                {
                    ;
                }
            }, accel, SensorManager.SENSOR_DELAY_GAME);

        //陀螺仪
        Sensor gyr = sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        sm.registerListener(new SensorEventListener() {
                TextView tv1 = findViewById(R.id.tv4);
                TextView tv2 = findViewById(R.id.tv5);
                TextView tv3 = findViewById(R.id.tv6);
                byte[] stream = new byte[12];
                byte[] order = new byte[]{0, 1, 2};//轴顺序纠正
                int[] dir = new int[]{1, 1, 1};//轴数值方向纠正
                @Override
                public void onSensorChanged(SensorEvent ev)
                {
                    tv1.setText(String.format("gyrX: %12.6f", ev.values[0]));
                    tv2.setText(String.format("gyrY: %12.6f", ev.values[1]));
                    tv3.setText(String.format("gyrZ: %12.6f", ev.values[2]));
                    //转换float数组为byte数组
                    int count = 0;
                    for (int i = 0; i < order.length; i++)
                    {
                        int value = Float.floatToIntBits(ev.values[order[i]] / 3.14159265250f * 180 * dir[i]);
                        stream[count++] = (byte)(value & 0xFF);
                        stream[count++] = (byte)((value >> 8) & 0xFF);
                        stream[count++] = (byte)((value >> 16) & 0xFF);
                        stream[count++] = (byte)((value >> 24) & 0xFF);
                    }
                    if (socketGyr != null)
                        socketGyr.send(stream);
                }
                @Override
                public void onAccuracyChanged(Sensor s, int accuracy)
                {
                    ;
                }
            }, gyr, SensorManager.SENSOR_DELAY_GAME);

        //欧拉角
        Sensor pry = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        sm.registerListener(new SensorEventListener() {
                TextView tv1 = findViewById(R.id.tv7);
                TextView tv2 = findViewById(R.id.tv8);
                TextView tv3 = findViewById(R.id.tv9);
                byte[] stream = new byte[12];
                byte[] order = new byte[]{2, 1, 0};//轴顺序纠正
                int[] dir = new int[]{-1, -1, -1};//轴数值方向纠正
                @Override
                public void onSensorChanged(SensorEvent ev)
                {
                    tv1.setText(String.format("pryX: %12.6f", ev.values[0]));
                    tv2.setText(String.format("pryY: %12.6f", ev.values[1]));
                    tv3.setText(String.format("pryZ: %12.6f", ev.values[2]));
                    //转换float数组为byte数组
                    int count = 0;
                    for (int i = 0; i < order.length; i++)
                    {
                        int value = Float.floatToIntBits(ev.values[order[i]] * 3.1415926535897f / 180 * dir[i]);
                        stream[count++] = (byte)(value & 0xFF);
                        stream[count++] = (byte)((value >> 8) & 0xFF);
                        stream[count++] = (byte)((value >> 16) & 0xFF);
                        stream[count++] = (byte)((value >> 24) & 0xFF);
                    }
                    if (socketPry != null)
                        socketPry.send(stream);
                }
                @Override
                public void onAccuracyChanged(Sensor s, int accuracy)
                {
                    ;
                }
            }, pry, SensorManager.SENSOR_DELAY_GAME);
/*
        //线性加速度
        Sensor lacc = sm.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
        sm.registerListener(new SensorEventListener() {
                TextView tv1 = findViewById(R.id.tv10);
                TextView tv2 = findViewById(R.id.tv11);
                TextView tv3 = findViewById(R.id.tv12);
                @Override
                public void onSensorChanged(SensorEvent ev)
                {
                    tv1.setText(String.format("laccX: %12.6f", ev.values[0]));
                    tv2.setText(String.format("laccY: %12.6f", ev.values[1]));
                    tv3.setText(String.format("laccZ: %12.6f", ev.values[2]));
                }
                @Override
                public void onAccuracyChanged(Sensor s, int accuracy)
                {
                    ;
                }
            }, lacc, SensorManager.SENSOR_DELAY_NORMAL);
*/
        //按键事件
        Button button = findViewById(R.id.button);
        button.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event)
                {
                    if (event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        //按下反馈
                        v.setAlpha(0.5f);
                    }
                    else if (event.getAction() == MotionEvent.ACTION_UP)
                    {
                        //松开反馈
                        v.setAlpha(1.0f);
                        //事件
                        EditText etIp = findViewById(R.id.address);
                        EditText etPortGyr = findViewById(R.id.portGyr);
                        EditText etPortAcc = findViewById(R.id.portAcc);
                        EditText etPortPry = findViewById(R.id.portPry);
                        Button b = (Button)v;
                        if (!socketRun)
                        {
                            //字符串转整形一定要检查
                            try
                            {
                                //String ip = "192.168.236.129";
                                String ip = etIp.getText().toString();
                                int portGyr = Integer.valueOf(etPortGyr.getText().toString());
                                int portAcc = Integer.valueOf(etPortAcc.getText().toString());
                                int portPry = Integer.valueOf(etPortPry.getText().toString());
                                //开始连接
                                Toast.makeText(MainActivity.this, String.format("%s:%d/%d/%d",ip,portGyr,portAcc,portPry), Toast.LENGTH_SHORT).show();
                                socketGyr = new SocketClient(ip, portGyr, 1024, new SocketClient.Event() {
                                        @Override
                                        public void change(boolean b)
                                        {
                                            Toast.makeText(MainActivity.this, "gyr client " + b, Toast.LENGTH_SHORT).show();
                                        }
                                        @Override
                                        public void recv(byte[] b)
                                        {
                                            Toast.makeText(MainActivity.this, "gyr client recv " + b.length, Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                socketAcc = new SocketClient(ip, portAcc, 1024, new SocketClient.Event() {
                                        @Override
                                        public void change(boolean b)
                                        {
                                            Toast.makeText(MainActivity.this, "acc client " + b, Toast.LENGTH_SHORT).show();
                                        }
                                        @Override
                                        public void recv(byte[] b)
                                        {
                                            Toast.makeText(MainActivity.this, "acc client recv " + b.length, Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                socketPry = new SocketClient(ip, portPry, 1024, new SocketClient.Event() {
                                        @Override
                                        public void change(boolean b)
                                        {
                                            Toast.makeText(MainActivity.this, "pry client " + b, Toast.LENGTH_SHORT).show();
                                        }
                                        @Override
                                        public void recv(byte[] b)
                                        {
                                            Toast.makeText(MainActivity.this, "pry client recv " + b.length, Toast.LENGTH_SHORT).show();
                                        }
                                    });
                            }
                            catch (NumberFormatException e)
                            {
                                Toast.makeText(MainActivity.this, "格式错误", Toast.LENGTH_SHORT).show();
                                return false;
                            }
                            b.setText("关  闭");
                            socketRun = true;
                            //b.setEnabled(false);
                            //禁止改动
                            etIp.setEnabled(false);
                            etPortGyr.setEnabled(false);
                            etPortAcc.setEnabled(false);
                            etPortPry.setEnabled(false);
                        }
                        else
                        {
                            socketGyr.exit();
                            socketAcc.exit();
                            socketPry.exit();
                            b.setText("连  接");
                            socketRun = false;
                            //开放改动
                            etIp.setEnabled(true);
                            etPortGyr.setEnabled(true);
                            etPortAcc.setEnabled(true);
                            etPortPry.setEnabled(true);
                        }
                    }
                    //不返回true的话ACTION_DOWN之后的事件都会被丢弃
                    return true;
                }
            });
    }
}
